<template>
    <div class="tabs">
        <div
            class="tab-item"
            :class="{
                active: tabActive === index,
            }"
            v-for="(item, index) in materialList"
            :key="item.value"
            @click="onTabActive(index)"
        >
            {{ item.label }}
        </div>

        <div class="right-box">
            <div class="btn-group" v-if="isBatchDelete">
                <div class="btn-item" @click="onDelete">确认删除</div>
                <div class="btn-item btn-cancel" @click="onCancelBatchDelete">
                    取消
                </div>
            </div>
            <div class="btn-group" v-else>
                <label class="btn-item" @click="isBatchDelete = true">
                    批量删除
                </label>

                <template
                    v-if="
                        props.active == 1 && materialList[tabActive].value != -1
                    "
                >
                    <label class="btn-item">
                        <el-icon
                            class="is-loading"
                            color="#5085fb"
                            v-if="isUploading"
                        >
                            <Loading />
                        </el-icon>
                        {{ isUploading ? "上传中" : "上传" }}
                        <input
                            type="file"
                            hidden
                            :accept="materialList[tabActive].accept"
                            @change="onSelectFile"
                            :disabled="isUploading"
                            multiple
                        />
                    </label>
                    <!-- <label class="btn-item">批量上传</label> -->
                </template>

                <label
                    class="btn-item"
                    @click="uploadModelRef.show()"
                    v-if="materialList[tabActive].value == -1"
                >
                    上传
                </label>
            </div>

            <div class="search-box">
                <input v-model="keyword" placeholder="搜索关键词" />
                <div class="btn" @click="onSearch">
                    <img
                        :src="`${_oss_}images/admin/material-store/icon-search.png`"
                    />
                    搜索
                </div>
            </div>
        </div>
    </div>

    <div class="res-list" v-loading="isLoading">
        <el-empty
            :image-size="150"
            description="数据为空"
            v-if="renderList.length === 0"
        />
        <div
            class="res-item"
            v-for="item in renderList"
            :key="item.id"
            @click="isBatchDelete ? onDeleteCheck(item.id) : ''"
            v-else
        >
            <div class="cover_image">
                <img
                    class="origin_image"
                    v-if="![4, 5].includes(materialList[tabActive].value)"
                    :src="GetCoverImage(item).cover"
                />

                <div class="origin_image origin_bg" v-else>
                    <img
                        :src="`${_oss_}images/admin/material-store/icon-music.png`"
                        v-if="materialList[tabActive].value == 4"
                    />
                    <img
                        :src="`${_oss_}images/admin/material-store/icon-pdf.png`"
                        v-if="materialList[tabActive].value == 5"
                    />
                </div>

                <img
                    v-if="isBatchDelete"
                    class="icon-checked"
                    :src="`${_oss_}images/admin/material-store/${
                        deleteIds.includes(item.id)
                            ? 'icon-checked'
                            : 'icon-nochecked'
                    }.png`"
                />

                <div
                    class="preview"
                    @click="onPreview(GetCoverImage(item).origin)"
                    v-if="materialList[tabActive].value != -1 && !isBatchDelete"
                >
                    <div class="btn-view">查看</div>
                </div>
            </div>
            <div class="info">
                <div class="title" :title="item.show_name">
                    {{ item.show_name }}
                </div>

                <!-- <img
                    class="icon-download"
                    :src="`${_oss_}images/admin/material-store/icon-download.png`"
                    v-if="
                        materialList[tabActive].value != -1 && props.active == 1
                    "
                    @click="
                        onDownload(item.show_name, GetCoverImage(item).origin)
                    "
                /> -->
            </div>
        </div>
    </div>

    <div class="bottom">数量统计：{{ list.length }}个</div>

    <DialogVideoPreviewP ref="videoPreviewRef" />
    <DialogAudioPreviewP ref="audioPreviewRef" />
    <DialogPdfPreviewP ref="pdfPreviewRef" />
    <DialogAdminMaterialUploadModelP
        ref="uploadModelRef"
        @success="onModelSuccess"
    />
    <DialogDownloadP ref="downloadRef" />
</template>
<script setup>
import { useContent } from ".";
import { Loading } from "@element-plus/icons-vue";
import Viewer from "viewerjs";
import "viewerjs/dist/viewer.css";

const props = defineProps({
    active: {
        type: Number,
        default: 1,
    },
});
const videoPreviewRef = ref(null);
const pdfPreviewRef = ref(null);
const audioPreviewRef = ref(null);
const uploadModelRef = ref(null);
const downloadRef = ref(null);

const {
    tabActive,
    list,
    renderList,
    materialList,
    isLoading,
    isBatchDelete,
    isUploading,
    deleteIds,
    keyword,
    onSelectFile,
    onDelete,
    onSearch,
    onDeleteCheck,
    onCancelBatchDelete,
    onTabActive,
    GetCoverImage,
    GetRes,
    GetModel,
} = useContent();

await GetRes();

const onModelSuccess = () => {
    GetModel();
};

const onPreview = (url) => {
    if ([1, 6, 7].includes(materialList[tabActive.value].value)) {
        const image = new Image();
        image.src = url;
        new Viewer(image, {
            zIndex: 3000,
            transition: false,
            navbar: false,
            title: false,
            toolbar: {
                zoomIn: {
                    show: 4,
                    size: "large",
                },
                zoomOut: {
                    show: 4,
                    size: "large",
                },
                play: {
                    show: 4,
                    size: "large",
                },
                rotateLeft: {
                    show: 4,
                    size: "large",
                },
                rotateRight: {
                    show: 4,
                    size: "large",
                },
            },
        }).show();
    } else if (materialList[tabActive.value].value == 2) {
        videoPreviewRef.value.show(url);
    } else if (materialList[tabActive.value].value == 5) {
        pdfPreviewRef.value.show(url);
    } else if (materialList[tabActive.value].value == 4) {
        audioPreviewRef.value.show(url);
    }
};

const onDownload = (name, url) => {
    downloadRef.value.show(name, url);
};
</script>
<style lang="scss" scoped>
.right-box {
    position: absolute;
    right: 0;
    display: flex;
}

.tabs {
    position: relative;
    display: flex;
    align-items: center;
    width: calc(100% - 60px);
    height: 56px;
    border-bottom: 1px solid #f1f1f1;
    margin: 0 auto;

    .btn-group {
        display: flex;
        margin-right: 30px;

        .btn-item {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 80px;
            height: 30px;
            border-radius: 4px;
            border: 1px solid #5085fb;
            font-size: 14px;
            color: #5085fb;
            margin-left: 13px;
            line-height: 1em;
            cursor: pointer;

            &:first-of-type {
                margin-left: 0;
            }

            &.btn-cancel {
                border-color: #666666;
                color: #666666;
            }
        }
    }

    .tab-item {
        display: flex;
        align-items: center;
        height: 100%;
        margin-right: 34px;
        font-size: 16px;
        color: #999;
        cursor: pointer;

        &.active {
            position: relative;
            font-weight: 500;
            color: #333333;

            &::after {
                content: "";
                position: absolute;
                bottom: 0;
                left: 0;
                width: 100%;
                height: 2px;
                background: #5085fb;
            }
        }
    }

    .search-box {
        display: flex;
        width: 175px;
        height: 30px;

        input {
            width: 111px;
            height: 100%;
            border: none;
            outline: none;
            padding: 0 8px;
            border: 1px solid rgba(191, 191, 191, 0.5);
            border-radius: 6px 0 0 6px;
            font-size: 12px;

            &::placeholder {
                font-size: 12px;
                color: #999999;
            }
        }

        .btn {
            display: flex;
            align-items: center;
            justify-content: center;

            width: 64px;
            height: 30px;
            background: #5085fb;
            font-size: 14px;
            color: #fff;
            border-radius: 0 6px 6px 0;
            cursor: pointer;

            img {
                width: 14px;
                height: 14px;
                margin-right: 3px;
            }
        }
    }
}

.res-list {
    height: calc(100% - 56px - 60px);
    display: flex;
    flex-wrap: wrap;
    align-content: baseline;
    overflow: auto;
    padding: 26px 30px 0;

    .el-empty {
        margin: 0 auto;
    }

    .res-item {
        width: 222px;
        height: 175px;
        border-radius: 6px;
        background-color: #fff;
        border: 1px solid #f1f1f1;
        overflow: hidden;
        margin-bottom: 20px;
        margin-right: 25px;

        &:nth-of-type(4n) {
            margin-right: 0;
        }

        .cover_image {
            position: relative;
            width: 222px;
            height: 127px;
            cursor: pointer;

            &:hover {
                .preview {
                    opacity: 1;
                }
            }

            .icon-checked {
                position: absolute;
                top: 9px;
                left: 11px;
                width: 18px;
                height: 18px;
            }

            .origin_image {
                position: relative;
                width: 100%;
                height: 100%;
                object-fit: cover;

                &.origin_bg {
                    width: 100%;
                    height: 100%;
                    background-image: url("https://yzpt-resources.oss-cn-hangzhou.aliyuncs.com/web/images/admin/material-store/icon-bg.png");
                    background-size: 100% 100%;

                    display: flex;
                    align-items: center;
                    justify-content: center;

                    img {
                        width: 50px;
                        height: 50px;
                    }
                }
            }
            .preview {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                display: flex;
                align-items: center;
                justify-content: center;
                background: rgba(0, 0, 0, 0.2);
                opacity: 0;
                transition: all 0.2s;

                .btn-view {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    width: 56px;
                    height: 30px;
                    background: rgba(255, 255, 255, 0.2);
                    border-radius: 4px;
                    font-size: 14px;
                    color: #ffffff;
                }
            }
        }
    }

    .info {
        display: flex;
        padding: 15px 16px;

        .title {
            font-size: 14px;
            color: #333;
            width: 100%;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            cursor: default;
        }

        .icon-download {
            width: 14px;
            height: 14px;
            margin-left: 10px;
            flex-shrink: 0;
            cursor: pointer;
        }
    }
}

.bottom {
    padding: 0 20px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    height: 60px;
    font-size: 16px;
    color: #999999;
}
</style>
